package request

import (
	"encoding/json"
	"fmt"
	"maya-service/constant"
	"maya-service/utils"
)

type RetVipStatus struct {
	Code    int    `json:"code"`
	Msg     string `json:"msg"`
	Success bool   `json:"success"`
	Data    struct {
		Enough bool `json:"enough"` // 金额
	} `json:"data"`
}

// CheckUserVipStatus 检查用户vip状态
func CheckUserVipStatus(token string, env string) (bool, error) {
	configUrl := constant.TestConfigUrl + "/api/biz/ai/chat/checkAccountBalance"
	if env == "prod" {
		configUrl = constant.ProdConfigUrl + "/api/biz/ai/chat/checkAccountBalance"
	}

	type Body struct {
		SkillCode string `json:"skill_code"`
	}

	sendData, _ := json.Marshal(&Body{SkillCode: "quanxi"})
	rsp, err := DoRequest("POST", configUrl, string(sendData), map[string]string{
		"token": token,
	})
	if err != nil {
		return false, err
	}

	ret := &RetVipStatus{}
	err = json.Unmarshal([]byte(rsp), ret)
	if err != nil {
		return false, err
	}

	if ret.Code != 0 {
		return false, fmt.Errorf("CheckUserVipStatus ret:", utils.ToJsonString(ret))
	}
	return ret.Data.Enough, fmt.Errorf("CheckUserVipStatus ret:", utils.ToJsonString(ret))
}
